import Vue from 'vue'
import VueRouter from 'vue-router'
import System from '../views/system/System.vue'
import Register from '../views/register/Register.vue'
import Login from '../views/login/Login.vue'
import apis from '../http/apis/apis'


Vue.use(VueRouter)

const routes = [
    {
        path: '/register',
        name: 'Register',
        props: true,
        component: Register,
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
    },
    {
        path: '/',
        //重定向
        redirect: '/system/parkingList'
    },
    {
        path: '/system',
        name: 'System',
        props: true,
        component: System,
        children: [
            {
                path: 'ordersList/:id',
                name: 'OrdersList',
                props: true,
                component: () => import("../components/ordersList/OrdersList.vue"),
            },
            {
                path: 'parkingList/:id',
                name: 'ParkingList',
                props: true,
                component: () => import("../components/parkingList/ParkingList.vue"),
            },
            {
                path: 'person/:id',
                name: 'Person',
                props: true,
                component: () => import("../components/person/Person.vue"),
            },
            {
                path: 'pay/:id',
                name: 'Pay',
                props: true,
                component: () => import("../components/pay/Pay.vue"),
            },

        ]
    },
]

const router = new VueRouter({
    routes,
})

// 全局守卫
router.beforeEach(async (to, from, next) => {
    const { data } = await apis.login.isLogin();
    if (to.path == "/login" || to.path == "/register") {
        next();
    } else if (to.path == "/system/parkingList" || to.path == "/system/person/") {
        alert('你还未登录，请登录');
        next('/login');
    } else {
        if (data.type == 401) {
            alert('你还未登录，请登录');
            next('/login');
        } else {
            next();
        }
    }


})

export default router
